jQuery:使相应的文本框相互影响';s值
情况: 2个文本框,TextBoxA和TextBoxB。我将文本输入TextBoxA,它设置TextBoxB的值。现在,我需要能够编辑TextBoxB的值,并设置TextBoxA的值。这很简单,但我需要在一个页面上对多对文本框使用它 这就是我到目前为止所做的:jQuery:使相应的文本框相互影响';s值,jquery,textbox,Jquery,Textbox,情况: 2个文本框,TextBoxA和TextBoxB。我将文本输入TextBoxA,它设置TextBoxB的值。现在,我需要能够编辑TextBoxB的值,并设置TextBoxA的值。这很简单,但我需要在一个页面上对多对文本框使用它 这就是我到目前为止所做的: $(function() { $(':text').css("border", "1px solid #666"); // Remove for actual app // Sets value of c
$(function() {
$(':text').css("border", "1px solid #666"); // Remove for actual app
// Sets value of corresponding textbox by ID
$(':text').blur(function() {
$('#inputval_' + ($(this).attr("id"))).val($(this).val());
});
//Styles all inputs set to read only
$('input[readonly=readonly]').css("border", "none");
$('input[readonly=readonly]').click(function() {// on click allows edit of readonly inputs
if ($(this).attr("readonly") == true) {
$(this).removeAttr("readonly");
$(this).css("border", "1px solid #666");
}
$(this).blur(function() {//on blur sets readonly back to true
if ($(this).attr("readonly") == false) {
$(this).attr("readonly", "readonly");
$(this).css("border", "none");
$('#' + ($(this).attr(("id").remove("inputval_")))).val($(this).val());
}
});
});
});
以及:
除了使第二组文本框影响相应的文本框外,所有操作都有效。你能看出我做错了什么吗?我几乎是一个JavaScript/jQueryNoob(我大部分时间都花在CSS/XHTML上),所以请温和一点。我发现了这个问题。这很微妙。第二组的选择器需要更改为:
$('#' + ($(this).attr(("id").remove("inputval_")))).val($(this).val());
致:
$('#' + ($(this).attr(("id").remove("inputval_")))).val($(this).val());
$('#' + ($(this).attr("id")).replace("inputval_","")).val($(this).val());