javascript - jQuery .data() 值中包含空格的问题

标签 javascript jquery twitter-bootstrap

很多问题都与此类似,但我真的不明白我做错了什么。

所以我传递了具有以下形式的开放 Bootstrap 模式:

    <form>
      VIN: <br>
      <input type="text" id="updateVin" name="updateVin" value="">
      <br>
      Description: <br>
      <input type="text" id="updateDescription" name="updateDescription" value="">
      <br>
      Color: <br>
      <input type="text" id="updateColor" name="updateColor" value="">
      <br>
      Model: <br>
      <input type="text" id="updateModel" name="updateModel" value="">
      <br>
      Area ID: <br>
      <input type="text" id="updateAreaId" name="updateAreaId" value="">
      <br>
      Geofence ID: <br>
      <input type="text" id="updateGeofenceId" name="updateGeofenceId" value="">
      <br>
      <button class="btn btn-warning" id="updateVehicle">Update</button>
      &nbsp;
      <button class="btn btn-danger" id="deleteVehicle"> 
      Delete 
      </button>
   </form>

我想将同一模态的不同链接的输入值设置为不同的值。

我是这样实现的:

$(document).on("click", ".open-UpdateModal", function () {
        var vin = $(this).data('vin');
        var description = $(this).data('description');
        var color = $(this).data('color');
        var model = $(this).data('model');
        var areaId = $(this).data('areaid');
        var geofenceId = $(this).data('geofenceid');
        $(".update #updateVin").val( vin );
        $(".update #updateDescription").val( description );
        $(".update #updateColor").val( color );
        $(".update #updateModel").val( model );
        $(".update #updateAreaId").val( areaId );
        $(".update #updateGeofenceId").val( geofenceId );
        $('h4.modal-title').text( 'Updating ' + vin + ':');
    });

我正在从我的一个表中的表数据获取值:

<td>
    <a data-toggle="modal" href="#my_modal" class="open-UpdateModal"
    data-deletevin=<%= vehicles[i].vin %>
    data-vin=<%= vehicles[i].vin %> 
    data-description=<%= vehicles[i].description %> 
    data-color=<%= vehicles[i].color %> 
    data-model=<%= vehicles[i].model %> 
    data-areaid=<%= vehicles[i].areaId %> 
    data-geofenceid=<%= vehicles[i].geofenceId %>>
    <%= vehicles[i].vin %> </a>
</td>

这很完美!除此之外,如果数据描述等于“Toyota Rav4”,则输入值将仅为“Toyota”。除此之外,这很完美。

这是 jQuery 和空格的问题吗?还是我做错了什么?

我也尝试过使用 $(this).attr('data-description') 。

最佳答案

只需在值周围添加引号:

<a data-toggle="modal" href="#my_modal" class="open-UpdateModal"
data-deletevin="<%= vehicles[i].vin %>"
data-vin="<%= vehicles[i].vin %>"
data-description="<%= vehicles[i].description %>" 
data-color="<%= vehicles[i].color %>" 
data-model="<%= vehicles[i].model %>" 
data-areaid="<%= vehicles[i].areaId %>"
data-geofenceid="<%= vehicles[i].geofenceId %>">
<%= vehicles[i].vin %> </a>

关于javascript - jQuery .data() 值中包含空格的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31999035/

相关文章:

javascript - 使用 ES6,导入 tinymce 作为 npm 包

twitter-bootstrap - 如何使用 Bootstrap 3 实现这种桌面/移动布局?

javascript - jQuery:无法通过数据查找元素

javascript - selenium-webdriver 与 Safari - 错误 : Server terminated early with status 1

jquery - 创建一个固定的导航,在滚动一小段距离后滑开并在向上滚动时返回

twitter-bootstrap - Bootstrap 推拉不对齐

twitter-bootstrap - 使 Twitter Bootstrap Table 最右边的列与边缘齐平

javascript - 如何检查 jQuery 插件中是否存在某个元素?

javascript - 用于仅显示项目的 Oracle APEX 格式掩码

javascript - 防止脚本在执行时运行两次