简体   繁体   English

如何使用jQuery的prop函数获取文本框的值

[英]How to get the value of a text box using jQuery's prop function

i am trying to get a value of some text box but its not working , more over the prop function isnt working also . 我试图得到一些文本框的值,但它不起作用,更多的prop功能也不能正常工作。 All i want is to enable the elements when the checkbox is checked and then show the value inside the textbox in alert window 我只想在选中复选框时启用元素,然后在警报窗口的文本框中显示值

Link to jsfiddle --> http://jsfiddle.net/k0cfj48L/5/ 链接到jsfiddle-> http://jsfiddle.net/k0cfj48L/5/

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
    <tr>
 <td class="td" bgcolor="#CCCCCC"> <input type="text" class="chkEdit" disabled /></td>

 <td class="td" bgcolor="#CCCCCC"> <input type="checkbox" class="chkView" value="Edit" /></td>

 <td class="td" bgcolor="#CCCCCC"> <input type="submit" class="chkUpdate" onclick="check();" disabled /></td>
    </tr>
</table>

JS: JS:

$(document).on('change','.chkView',function(){
    var row = $(this).closest('tr');
    if($(this).is(':checked')) {          
        $(row).find('.chkEdit,.chkUpdate').prop("disabled",false); 
        //var x = $(row).find('.chkEdit').val();   
    }else{
        $(row).find('.chkEdit,.chkUpdate').prop("disabled",true);     
    }
});

/////////////////////////////////////////////////////////
function check()    {
    var row = $(this).closest('tr');
    var x =$(row).find('.chkEdit').val();
    alert(x);
    return ;
}

The jsfiddle is edited the enable function is working but i still cant get the alert jsfiddle被编辑,启用功能正在工作,但我仍然无法得到警报

http://jsfiddle.net/k0cfj48L/2/ http://jsfiddle.net/k0cfj48L/2/

I have fixed your fiddle. 我已经修理了你的小提琴。

1) JQuery was not included, so '$' in $(document).ready() was not defined. 1)不包含JQuery,因此未定义$(document).ready()中的“ $”。

2) I changed the JS to be included in the head so your check function is read as defined on execution of the page. 2)我更改了要包含在头部中的JS,以便按照页面执行时定义的方式读取您的check函数。

3) Your check function is not being instantiated directly within an event, so $(this) does nothing, rather is just a window reference. 3)您的检查功能不是直接在事件中实例化的,因此$(this)不执行任何操作,而只是一个窗口引用。 I incldued a proper selection of the textarea, but I'd consider using IDs or adding a jQuery event on the submit button and then finding a sibling: 我对textarea进行了适当的选择,但是我会考虑使用ID或在Submit按钮上添加jQuery事件,然后找到一个同级对象:

$(document).on('change','.chkView',function(){
    var row = $(this).closest('tr');
    if($(this).is(':checked'))
    {          
        $(row).find('.chkEdit,.chkUpdate').prop("disabled",false); 
        //var x = $(row).find('.chkEdit').val();   
    }
    else
    {
        $(row).find('.chkEdit,.chkUpdate').prop("disabled",true);     
    }
});

/////////////////////////////////////////////////////////
function check()    
  {
      var x = $('.chkEdit').val();
       alert(x);
       return ;
  } 

jsFiddle demo jsFiddle演示

in your HTML pass the this reference as fn argument! 在您的HTML中this引用作为fn参数传递!

onclick="check(this);"

Disclaimer of liability : I'm not suggesting to use inline JS at all - read further! 免责声明 :我完全不建议使用内联JS-请继续阅读!

than in jQuery: 比jQuery中:

$(document).on('change','.chkView', function(){  
    $(this).closest('tr').find('.chkEdit, .chkUpdate').prop("disabled", !this.checked);    
});
/////////////////////////////////////////////////////////
function check(el){ // `el` is the `this` element argument you've passed in HTML
    var x = $(el).closest('tr').find('.chkEdit').val();
    return alert( x );
}

Also instead of using lnline JS (inside HTML) which makes programmers life really hard, 另外,也不要使用内联JS(内部HTML)来增加程序员的生活负担,
do it all in JS! 用JS完成所有操作! jsFiddle demo jsFiddle演示

$('.chkUpdate').on("click", function(e){
    e.preventDefault();
    var x = $(this).closest('tr').find('.chkEdit').val();
    alert( x );
});

Note that if your table is created dynamically you might want to use the dynamic event delegation like: 请注意 ,如果表是动态创建的 ,则可能需要使用动态事件委托,例如:

$('.someNotDynamicParent').on("click", ".chkUpdate", function(e){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM