[英]Extracting string between html tags
In my CakePHP view file, I have two elements 在我的CakePHP视图文件中,我有两个元素
echo $this->Form->create('Shift', array('id' => 'myform'));
echo $this->Form->input('user_id', array('id' => 'my_user_id', 'type'=>'text'));
and some Javascript that confirms something for the user. 以及一些可以为用户确认某些内容的Javascript。
<?php echo $this->Html->scriptBlock('
$(document).ready(function() {
$("#myform").submit(function(event) {
alert("Creating shift for " + $("#my_user_id").val());
});
});
');
?>
So if my HTML looks like this: 因此,如果我的HTML看起来像这样:
<div class="input select required">
<label for="my_user_id">User</label>
<select name="data[Shift][user_id]" id="my_user_id" required="required">
<option value="1">john doe</option>
<option value="2">john johnson</option>
how can I access the values between the option tags? 如何访问选项标签之间的值? As in, "john doe" and "john johnson"? 如“约翰·杜伊”和“约翰·约翰逊”?
You can get text
of option
using each()
on option 您可以使用option
each()
获取option
text
$("#my_user_id option").each(function(){
alert($(this).text());
});
If you want selected text then use option:selected 如果要选择文本,请使用option:selected
$("#my_user_id option:selected").text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.