Javascript 如何选择无引号的html属性值?
我有一个multiple-select元素,我动态地将它附加到表单中 从一个隐藏的领域。我已跳过元素中的空格 这是我隐藏的领域Javascript 如何选择无引号的html属性值?,javascript,jquery,html,Javascript,Jquery,Html,我有一个multiple-select元素,我动态地将它附加到表单中 从一个隐藏的领域。我已跳过元素中的空格 这是我隐藏的领域 <input type="hidden" id="field1" name="field1" value="<select id=multipleselectid1 name=multipleselectid1 multiple><option value=All Balance Types>All Ba
<input type="hidden" id="field1" name="field1" value="<select id=multipleselectid1 name=multipleselectid1 multiple><option value=All Balance Types>All Balance Types</option><option value=010>010 - Opening Ledger Balance</option><option value=011>011 - Average Opening Ledger MTD</option></select>" />
但未选择此值
我还实现了一个测试代码来检查是否选择了其他值。所以我试过这个密码
var elementvalue='010,011'; //-----------> This works
var arr = elementvalue.trim().split('\,');
$('#multipleselectid1').val(arr);
这段代码正在运行
为什么只有第一个值未被选中
这是上面的示例在选择框中从值中删除并在值中添加单引号:
$document.readyfunction{
$'myform'。追加$'field1'。val;
var elementvalue='All Balance Types';//--->这是有效的
var arr=elementvalue.trim.split'\,';
$'multipleselectid1'.valarr;
};
我能够通过使用单引号包装value属性,并在html中应该使用双引号的地方插入双引号,从而使fiddle工作 然而,我强烈建议远离将字符串html从页面的一部分附加到另一部分。它很容易受到XSS攻击
<input type="hidden" id="field1" name="field1" value='<select id="multipleselectid1" name="multipleselectid1" multiple><option value="All Balance Types">All Balance Types</option><option value="010">010 - Opening Ledger Balance</option><option value="011">011 - Average Opening Ledger MTD</option></select>' />
输入元素中的值被解释为string。您是否尝试使用var elevatalue='All Balance Types'而不是所有余额类型?@Roque是的,但仍然不起作用您确实不应该这样做。将HTML存储在另一个HTML元素的属性中是一个非常糟糕的主意。
<input type="hidden" id="field1" name="field1" value='<select id="multipleselectid1" name="multipleselectid1" multiple><option value="All Balance Types">All Balance Types</option><option value="010">010 - Opening Ledger Balance</option><option value="011">011 - Average Opening Ledger MTD</option></select>' />