JavaScript 中的奇怪字符串连接

weird string concat in JavaScript

我对 jQuery 中的字符串连接感到奇怪,如下所示:

      var name = button.data('name')
      var emailName = button.data('email')
      var userName = button.data('username')
      var userStatus = button.data('active')
      var userId = button.data('id')
      alert(userId)
      var actionUpdate = "{{action('UserController@update', "+userId+")}}" 
      alert(actionUpdate)

和 result/alert 的 userId 是 28723050-71e9-11e7-a0a9-e9f620359699(uuid 作为用户 ID)但是当我 concated/joined 时,我得到的 actionUpdate 变量是 {{action('UserController@update', "userId")}} 而不是我预期的 {{action('UserController@update', "28723050-71e9-11e7-a0a9-e9f620359699")}}。

我尝试用 concat 或 += 连接那些字符串和变量,但没有得到预期的结果。

非常感谢任何替代方案和解决方案。

如果出于某种奇怪的原因,您真正想要的是

{{action('UserController@update', "28723050-...9699")}}

UserController@update 周围加上单引号,在 userId 周围加上双引号,我想你需要

  var userId = button.data('id')
  alert(userId)
  var actionUpdate = "{{action('UserController@update', \""+userId+"\")}}" 
  alert(actionUpdate)

(即,您需要在连接部分的两边添加一个转义的双引号字符。)

谢谢 John,你对 "escaped char" 的想法,现在这个工作正如我所料,我猜 "{,{,},}" 在 JS 或 Jquery 中有特殊含义,所以我将这一行写为:

var actionUpdate = "{{action('UserController@update', '" + userId + "')}}"

希望它对想要将表单提交操作更改为 laravel 控制器的人有用。