我有以下链接,点击它时,它正确地将onclick事件中存储的html添加到页面中。
链接:
<a class="button small secondary add_confirmation_button" href="#" onclick="add_fields(this, "confirmations", "<div class=\"fields\">\n <fieldset>\n <legend>Confirmation<\/legend>\n <div class=\"row\">\n <div class=\"medium-6 columns\">\n <div class=\"input select optional confirmation_builder_confirmations_responder_id\"><label class=\"select optional\" for=\"confirmation_builder_confirmations_attributes_new_confirmations_responder_id\">Responder<\/label><select class=\"select optional\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][responder_id]\" id=\"confirmation_builder_confirmations_attributes_new_confirmations_responder_id\"><option value=\"\">Select Responder<\/option>\n <option value=\"1\">Suncoast Credit Union<\/option>\n <option value=\"2\">State Street Bank and Trust Company<\/option>\n <option value=\"3\">AuburnBank<\/option>\n <option value=\"4\">Robertson Banking Company<\/option>\n <option value=\"5\">Phenix-Girard Bank<\/option>\n <option value=\"select_responder\">Select a responder<\/option><\/select><\/div>\n <\/div>\n <div class=\"medium-6 columns\">\n <div class=\"input date optional confirmation_builder_confirmations_confirm_as_of_close_on\"><label class=\"date optional\" for=\"confirmation_builder_confirmations_attributes_new_confirmations_confirm_as_of_close_on_2i\">Confirm as of close on<\/label><select id=\"confirmation_builder_confirmations_attributes_new_confirmations_confirm_as_of_close_on_2i\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][confirm_as_of_close_on(2i)]\" class=\"date optional\">\n <option value=\"1\">January<\/option>\n <option value=\"2\">February<\/option>\n <option value=\"3\">March<\/option>\n <option value=\"4\">April<\/option>\n <option value=\"5\">May<\/option>\n <option value=\"6\" selected=\"selected\">June<\/option>\n <option value=\"7\">July<\/option>\n <option value=\"8\">August<\/option>\n <option value=\"9\">September<\/option>\n <option value=\"10\">October<\/option>\n <option value=\"11\">November<\/option>\n <option value=\"12\">December<\/option>\n <\/select>\n <select id=\"confirmation_builder_confirmations_attributes_new_confirmations_confirm_as_of_close_on_3i\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][confirm_as_of_close_on(3i)]\" class=\"date optional\">\n <option value=\"1\">1<\/option>\n <option value=\"2\">2<\/option>\n <option value=\"3\">3<\/option>\n <option value=\"4\">4<\/option>\n <option value=\"5\">5<\/option>\n <option value=\"6\">6<\/option>\n <option value=\"7\">7<\/option>\n <option value=\"8\">8<\/option>\n <option value=\"9\">9<\/option>\n <option value=\"10\">10<\/option>\n <option value=\"11\">11<\/option>\n <option value=\"12\">12<\/option>\n <option value=\"13\">13<\/option>\n <option value=\"14\">14<\/option>\n <option value=\"15\">15<\/option>\n <option value=\"16\">16<\/option>\n <option value=\"17\">17<\/option>\n <option value=\"18\" selected=\"selected\">18<\/option>\n <option value=\"19\">19<\/option>\n <option value=\"20\">20<\/option>\n <option value=\"21\">21<\/option>\n <option value=\"22\">22<\/option>\n <option value=\"23\">23<\/option>\n <option value=\"24\">24<\/option>\n <option value=\"25\">25<\/option>\n <option value=\"26\">26<\/option>\n <option value=\"27\">27<\/option>\n <option value=\"28\">28<\/option>\n <option value=\"29\">29<\/option>\n <option value=\"30\">30<\/option>\n <option value=\"31\">31<\/option>\n <\/select>\n <select id=\"confirmation_builder_confirmations_attributes_new_confirmations_confirm_as_of_close_on_1i\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][confirm_as_of_close_on(1i)]\" class=\"date optional\">\n <option value=\"2015\" selected=\"selected\">2015<\/option>\n <option value=\"2014\">2014<\/option>\n <option value=\"2013\">2013<\/option>\n <option value=\"2012\">2012<\/option>\n <option value=\"2011\">2011<\/option>\n <option value=\"2010\">2010<\/option>\n <option value=\"2009\">2009<\/option>\n <option value=\"2008\">2008<\/option>\n <option value=\"2007\">2007<\/option>\n <option value=\"2006\">2006<\/option>\n <option value=\"2005\">2005<\/option>\n <\/select>\n <\/div>\n <\/div>\n <\/div>\n <div class=\"row\">\n <div class=\"medium-6 columns\">\n <div class=\"input select optional confirmation_builder_confirmations_client_id\"><label class=\"select optional\" for=\"confirmation_builder_confirmations_attributes_new_confirmations_client_id\">Client<\/label><select class=\"select optional\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][client_id]\" id=\"confirmation_builder_confirmations_attributes_new_confirmations_client_id\"><option value=\"\">Select Client<\/option>\n <option value=\"33\">A com<\/option>\n <option value=\"1\">Boars Head<\/option>\n <option value=\"34\">Bobs co<\/option>\n <option value=\"32\">Dogs<\/option>\n <option value=\"24\">Example Company<\/option>\n <option value=\"new_client\">Add a new client<\/option><\/select><\/div>\n <\/div>\n <div class=\"medium-6 columns\">\n <div class=\"input select optional confirmation_builder_confirmations_signer_id\"><label class=\"select optional\" for=\"confirmation_builder_confirmations_attributes_new_confirmations_signer_id\">Signer<\/label><select class=\"select optional\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][signer_id]\" id=\"confirmation_builder_confirmations_attributes_new_confirmations_signer_id\"><option value=\"\">Select Signer<\/option>\n <option value=\"1\">Michael mickson<\/option>\n <option value=\"2\">t<\/option>\n <option value=\"3\">t<\/option>\n <option value=\"4\">t<\/option>\n <option value=\"5\">t<\/option>\n <option value=\"6\">t<\/option>\n <option value=\"7\">j<\/option>\n <option value=\"8\">j<\/option>\n <option value=\"9\">j<\/option>\n <option value=\"10\">joel<\/option>\n <option value=\"11\">d<\/option>\n <option value=\"12\">d<\/option>\n <option value=\"13\">d<\/option>\n <option value=\"14\">d<\/option>\n <option value=\"15\">d<\/option>\n <option value=\"16\">t<\/option>\n <option value=\"17\">t<\/option>\n <option value=\"18\">t<\/option>\n <option value=\"19\">joel<\/option>\n <option value=\"20\">joel<\/option>\n <option value=\"21\">joel<\/option>\n <option value=\"22\">j<\/option>\n <option value=\"23\">jo<\/option>\n <option value=\"24\">Michelle <\/option>\n <option value=\"25\">j<\/option>\n <option value=\"29\">MJ<\/option>\n <option value=\"30\">MJ<\/option>\n <option value=\"31\">John<\/option>\n <option value=\"32\">d<\/option>\n <option value=\"33\">Michael<\/option>\n <option value=\"34\">Michelle<\/option>\n <option value=\"35\">j<\/option>\n <option value=\"36\">b<\/option>\n <option value=\"new_signer\">Add a new signer<\/option><\/select><\/div>\n <\/div>\n <\/div>\n <fieldset>\n <legend>Account\'s<\/legend>\n <p>\n <a class=\"button small secondary\" href=\"#\" onclick=\"add_fields(this, &quot;accounts&quot;, &quot;&lt;div class=\\&quot;fields\\&quot;&gt;\\n &lt;div class=\\&quot;row\\&quot;&gt;\\n &lt;div class=\\&quot;medium-4 columns\\&quot;&gt;\\n &lt;div class=\\&quot;input string required confirmation_builder_confirmations_new_confirmations_accounts_account_id\\&quot;&gt;&lt;label class=\\&quot;string required\\&quot; for=\\&quot;confirmation_builder_confirmations_attributes_new_confirmations_accounts_attributes_new_accounts_account_id\\&quot;&gt;&lt;abbr title=\\&quot;required\\&quot;&gt;*&lt;\\/abbr&gt; Account Number&lt;\\/label&gt;&lt;input class=\\&quot;string required\\&quot; type=\\&quot;text\\&quot; name=\\&quot;confirmation_builder[confirmations_attributes][new_confirmations][accounts_attributes][new_accounts][account_id]\\&quot; id=\\&quot;confirmation_builder_confirmations_attributes_new_confirmations_accounts_attributes_new_accounts_account_id\\&quot; /&gt;&lt;\\/div&gt;\\n &lt;\\/div&gt;\\n &lt;div class=\\&quot;medium-4 columns\\&quot;&gt;\\n &lt;div class=\\&quot;input select optional confirmation_builder_confirmations_new_confirmations_accounts_style\\&quot;&gt;&lt;label class=\\&quot;select optional\\&quot; for=\\&quot;confirmation_builder_confirmations_attributes_new_confirmations_accounts_attributes_new_accounts_style\\&quot;&gt;Confirmation style&lt;\\/label&gt;&lt;select class=\\&quot;select optional\\&quot; name=\\&quot;confirmation_builder[confirmations_attributes][new_confirmations][accounts_attributes][new_accounts][style]\\&quot; id=\\&quot;confirmation_builder_confirmations_attributes_new_confirmations_accounts_attributes_new_accounts_style\\&quot;&gt;&lt;option selected=\\&quot;selected\\&quot; value=\\&quot;liability\\&quot;&gt;Liability&lt;\\/option&gt;\\n &lt;option value=\\&quot;asset\\&quot;&gt;Asset&lt;\\/option&gt;\\n &lt;option value=\\&quot;line_of_credit\\&quot;&gt;Line of credit&lt;\\/option&gt;&lt;\\/select&gt;&lt;\\/div&gt;\\n &lt;\\/div&gt;\\n &lt;div class=\\&quot;medium-4 columns\\&quot;&gt;\\n &lt;label for=\\&quot;Remove\\&quot;&gt;Remove&lt;\\/label&gt;&lt;input type=\\&quot;hidden\\&quot; value=\\&quot;false\\&quot; name=\\&quot;confirmation_builder[confirmations_attributes][new_confirmations][accounts_attributes][new_accounts][_destroy]\\&quot; id=\\&quot;confirmation_builder_confirmations_attributes_new_confirmations_accounts_attributes_new_accounts__destroy\\&quot; /&gt;&lt;a class=\\&quot;button tiny alert expand\\&quot; href=\\&quot;#\\&quot; onclick=\\&quot;remove_fields(this); return false;\\&quot;&gt;- Remove account&lt;\\/a&gt;\\n &lt;\\/div&gt;\\n &lt;\\/div&gt;\\n&lt;\\/div&gt;&quot;); return false;\">+ Add account<\/a>\n <\/p>\n <\/fieldset>\n <input type=\"hidden\" value=\"false\" name=\"confirmation_builder[confirmations_attributes][new_confirmations][_destroy]\" id=\"confirmation_builder_confirmations_attributes_new_confirmations__destroy\" /><a class=\"button tiny alert expand no_bottom_margin\" href=\"#\" onclick=\"remove_fields(this); return false;\">- Remove confirmation<\/a>\n <\/fieldset>\n<\/div>"); return false;">+ Add confirmation</a>
无法弄清楚如何在onclick属性中为select选项附加一个select选项。
如何解决这个问题?
我想找到这个:
<option value=\"\">Select Client<\/option>\n
并将其替换为:
<option value=\"\">Select Client<\/option>\n<option value=\"4\">Client with id of 4<\/option>\n
另一种想法
除了我提到的查找和替换方法;我认为可能有一种方法可以将jquery转换为&#34;&#34;我可以使用典型的jquery选择器。然后,我会找到选择并附加选项,然后将其转换回原来的转义*状态。
*不确定转义是否是正确的词。
作为开发人员,我不知道如何引用字符串的当前状态是一个问题。它是逃脱的,没有转义吗?
答案 0 :(得分:0)
而不是使用正则表达式并尝试附加到现有的onclick
函数,将其保存在字符串上更容易,然后在完成后使用.prop
或.attr
更改onclick
功能..
var before_option = "add_fields(this, "confirmations", ...";
var after_option = "... <option value=\"33\">A com<\/option>\n <option value=\"1\">Boars Head<\/option>\n";
//Let's say you need to click a button to add that option
$("#click_button").click(function(){
var options = "<option value=\"\">Select Client<\/option>\n<option value=\"4\">Client with id of 4<\/option>\n";
var final_option = before_option + options + after_option;
$(".add_confirmation_button").prop("onclick", final_option);
});