我希望有人可以帮我解决我遇到的问题。我正在尝试根据categoryname(输入文本)字段中自动填充的值自动填充价格(输入文本)字段。我知道如何使用onchange函数,因为我在我的php页面中的许多其他元素上成功使用它。但是,这个更具挑战性,因为这两个字段都是只读的。此外,在这种情况下使用按钮不是一个选项,因此与我的问题相关的其他一些帖子对我不起作用。
我尝试过以下方法: Set dropdown value based on what is selected in another dropdown using Javascript,jQuery's change() event doesn't work as expected with input text fields. This, however, does work.,Text changed event of a readonly textbox ,JS/jQuery: fire onchange from a disabled/readonly input without using button以及其他10个人。不幸的是,正如我所提到的,我在这些帖子中试过的代码方案都没有为我工作。 (例如绑定,输入,触发等)
以下是我目前的代码(仍然无效): 的 PHP
echo '<td align="left" width="8%" height="25px">';
$ddlquery4 = "SELECT * FROM dvd ORDER BY title ASC";
$ddlresult4 = mysqli_query($dbc, $ddlquery4) or die("Bad SQL: $ddlquery4");
echo '<select type="text" class="dropdown" name="dvdid" id="movieID" size="1" onchange="updatecat()">';
echo '<option value=""></option>';
while($ddlrow4=mysqli_fetch_array($ddlresult4, MYSQLI_ASSOC)){
echo "<option value='".$ddlrow4['dvdid']."' data-categoryname='".$ddlrow4['categoryname']."'>" . $ddlrow4['title'] . "</option>";
} //End while statement
echo "</select>";
echo '</a></td>';
echo '<td align="left" width="5%">';
echo '<input style="font-size:20px; border:0px; background:none; box-shadow:none;" type="text" id="catName" name="categoryname" readonly="readonly" onchange="popprice()">';
echo '<td align="left" width="4%">';
'<input type="text" id="catprice" name="price" readonly="readonly">';
JS / jquery的
<script>
$('#movieID').change(function() {
selectedOption = $('option:selected', this);
$('input[id=catName]').val( selectedOption.data('categoryname') );
});
</script>
<script>
$('#catName').bind('change', function() {
if (document.getElementById('catName').value == 'Regular')
document.getElementById('catprice').value = '.50';
else if (document.getElementById('catName').value == 'Popular')
document.getElementById('catprice').value = '1.00';
else if (document.getElementById('catName').value == 'Current Release')
document.getElementById('catprice').value = '1.50';
else if (document.getElementById('catName').value == 'Current Hit')
document.getElementById('catprice').value = '2.00';
});
</script>
我试过的其他数字小样本的小样本:
<script>
$(document).ready(function() {
$('popprice').change(function() {
selectedOption = $('option:selected', .data('categoryname');
if $(categorynameValue).val('Regular') {
('#catprice').val('.50');
}
else if $(categorynameValue).val('Popular') {
('#catprice').val('1.00');
}
else if $(categorynameValue).val('Current Release') {
('#catprice').val('1.50');
}
else if $(categorynameValue).val('Current Hit') {
('#catprice').val('2.00');
}
else ('#catprice').val("");
});
});
</script>
将popprice添加到选择行中的updatecat更改函数,如下所示:
echo '<td align="left" width="8%" height="25px">';
$ddlquery4 = "SELECT * FROM dvd ORDER BY title ASC";
$ddlresult4 = mysqli_query($dbc, $ddlquery4) or die("Bad SQL: $ddlquery4");
echo '<select type="text" class="dropdown" name="dvdid" id="movieID" size="1" onchange="updatecat(); popprice();">';
echo '<option value=""></option>';
while($ddlrow4=mysqli_fetch_array($ddlresult4, MYSQLI_ASSOC)){
echo "<option value='".$ddlrow4['dvdid']."' data-categoryname='".$ddlrow4['categoryname']."'>" . $ddlrow4['title'] . "</option>";
} //End while statement
我尝试在#movieID更改函数中组合if elseif语句,并且只保留onchange =“updatecat();”在select元素中:
<script>
$(document).ready(function() {
$('#movieID').change(function() {
selectedOption = $('option:selected', this);
$('input[id=catName]').val(selectedOption.data('categoryname') );
var selectedOption = $('option:selected', .data('categoryname') );
if $(categorynameValue).val('Regular') {
('#catprice').val('.50');
}
else if $(categorynameValue).val('Popular') {
('#catprice').val('1.00');
}
else if $(categorynameValue).val('Current Release') {
('#catprice').val('1.50');
}
else if $(categorynameValue).val('Current Hit') {
('#catprice').val('2.00');
}
else ('#catprice').val("");
});
});
</script>
我也在movieID更改函数脚本中尝试了这个...
if ('#catName' = 'Regular') {
document.getElementByID("catprice".innerHTML = ".05"
} else if ('#catName' = 'Popular') {
document.getElementByID("catprice".innerHTML = "1.00"
} else if ('#catName' = 'Current Release'){
document.getElementByID("catprice".innerHTML = "1.50"
} else if ('#catName' = 'Current Hit'){
document.getElementByID("catprice".innerHTML = "2.00"
非常感谢任何建议,我提前感谢您的时间并分享您的知识。