【问题标题】:JQuery change input type hidden valueJQuery改变输入类型隐藏值
【发布时间】:2015-10-20 17:56:43
【问题描述】:

我用 DOM 附加按钮更改隐藏在 JQuery 上的输入类型,代码看起来像

$("body").on('click', '.companyContractButt', function(){
    var id = $(this).attr('data-id');
    var what = $(this).attr('data-what');
    alert($(".selectedWhatis[data-id='"+id+"']").attr('data-id'));
    $(".selectedWhatis[data-id='"+id+"']").val(what);
    alert($(".selectedWhatis[data-id='"+id+"']").val());
});

和输入类型隐藏的样子

<input type='hidden' data-id='".$contract->id."' class='selectedWhatis' value='hovno'>  

但是当我提醒输入类型隐藏时,我总是无法定义......我不知道为什么。 var id 和 var what 显示正确的值..

有创建DOM按钮的功能

$("body").on('click', '.clickChange', function(){
var id = $(this).attr('data-id');
var nm = $(this).attr('data-name');

$(".filling[data-id='"+id+"']").html('');
$(".filling[data-id='"+id+"']").html("<button class='companyContractButt' data-what='create' data-id='"+id+"'><span>"+$("#createTranslate").val()+"</span></button> <button class='companyContractButt' data-what='append' data-id='"+id+"'><span>"+$("#appendTranslate").val()+"</span></button><br /><input type='text' style='width: 200px; margin-top: 5px;' data-id='"+id+"' value='"+nm+"' class='inputingPerson'>");
$(".inputingPerson[data-id='"+id+"']").focus();
}); 

【问题讨论】:

  • 你可以试试alert($("input[class=selectedWhatis][data-id='"+id+"']").val());
  • 仍然未定义:-/,但everythink都有正确的值,只是显示未定义,我不明白这个错误....
  • alert($('.selectedWhatis[data-id='+id+']').val());请试试
  • 请告诉我们.companyContractButt按钮的html代码
  • 我认为您按钮的 data-idhidden 字段的 data-id 不同步。

标签: javascript jquery


【解决方案1】:

demo

var id = 5

$('.selectedWhatis').attr('data-id', id);


alert($('.selectedWhatis[data-id='+id+']').val())

查看此示例并尝试一下。我只是为此使用''

【讨论】:

  • 在小提琴中它会提醒 id 值吗?也许还有其他问题?
  • 不,我检查每个值,错误是当我得到隐藏输入类型的值时,看起来 JS 看不到那个元素
  • 它应该如您在小提琴中看到的那样提醒hovno的值
  • 我编辑了帖子,添加了添加 DOM 按钮的功能,也许会有所帮助。
  • @user5366720:这alert($(".selectedWhatis[data-id='"+id+"']").attr('data-id')); 是否正确提醒隐藏输入的数据ID?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 2011-05-21
相关资源
最近更新 更多