[英]Get option title from dropdown list with javascript
I got a dynamic input form from a dropdown list. 我从下拉列表中获得了动态输入表单。 Here's my code for the dropdown
这是我的下拉代码
<label class="strong">Cut:
<select name="add_qty">
<?php
$qqs = mysqli_query($conn, "SELECT * FROM m_maincut WHERE maincut_status = '1'");
while($roq = mysqli_fetch_array($qqs, MYSQLI_ASSOC))
{
?>
<option id="opsi" value="<?php echo $roq['maincut_id']; ?>" title="<?php echo $roq['maincut_name']; ?>"><?php echo $roq['maincut_name']; ?></option>
<?php
}
?>
</select>
</label>
Total: <input type="text" name="add_name" />
<input onclick="addRow(this.form);" type="button" value="Add New" />
and here's my javascript to make the form dynamic (click button for add more option) 这是我的JavaScript,使表格具有动态性(单击按钮可添加更多选项)
<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
rowNum++;
var text = frm.opsi.title;
var row = '<p id="rowNum' + rowNum + '">Nama Biaya: <option type="text" name="qty[]" size="4" value="' + frm.add_qty.value + '">' + text + '</option> Jumlah: <input type="text" name="name[]" value="' + frm.add_name.value + '"> <input type="button" value="Remove" onclick="removeRow(' + rowNum + ');"></p>';
jQuery('#itemRows').append(row);
frm.add_qty.value = '';
frm.add_name.value = '';
}
function removeRow(rnum) {
jQuery('#rowNum' + rnum).remove();
}
</script>
Here's my result: 这是我的结果:
"Tambah ke Biaya" means "Add More" “ Tambah ke Biaya”的意思是“添加更多”
My question is , how to get the title of the option? 我的问题是 ,如何获得期权的标题? I can get the value, but I can get the title to show up to client.
我可以获取值,但可以获取标题以显示给客户。 Because if I show the value, it will show only number (the ID's)
因为如果我显示该值,它将仅显示数字(ID)
You have to get innerHTML of the option tag if you want to get which is shown to the user. 如果要获取显示给用户的选项,则必须获取option标签的innerHTML。 And you must create an option instead ap element
并且您必须创建一个选项而不是ap元素
To get the title attribute of tag, 要获取标签的title属性,
var title = jQuery(this).attr('title');
To get html inside <option>
, 要在
<option>
获取html,
var html = jQuery(this).html();
And suggest you to use jQuery, it will be easier 并建议您使用jQuery,它将更容易
<input id="add_new" type="button" value="Add New" />
and trigger click event of this button like, 并触发此按钮的点击事件,例如,
jQuery('#add_new').click(function(){
// your process
});
如果您确实在寻找标题的值,因为title是元素的属性之一,那么您应该可以使用
jQuery('your-selector').attr('title');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.