forms - jQuery use load() with param -
exactly want trick in html. hope can use jquery load html in block( <span></span>
). , in target html, need params in url something. a.html main page, b.html page embed in block of a.html, in a.html, there block this:
<span id="rp"></span>
and in its' javascript, thing( asked question of title):
var param="x="+1+"&y="+2; $("#rp").load("b.html?"+param);
it's main page do, , in b.html,
it pass params in url fill form of b.html.
in b.html, there form as:
<form id="myform" action="192.168.1.xxx/goal.html" method="post"> <input type="hidden" id="xs" name="x" /> <input type="hidden" id="ys" name="y" /> <input type="submit" value="test" /> </form>
(the goal.html in domain name, cannot use ajax in main page. that's reason why come way use method 'post' send data goal.html)
and javascript of b.html is:
window.onload=function(){ function querystr(str) { var query = window.location.search.substring(1); var vars = query.split("&"); var val=""; (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if (pair[0]==str) { val=pair[1]; } } return val; } //var test=setinterval(fillform,100); fillform(); function fillform(){ if(isfilled()){ document.getelementbyid("xs").value=querystr("x"); document.getelementbyid("ys").value=querystr("y"); //clearinterval(test); //document.getelementbyid("myform").submit(); } } function isfilled(){ if(querystr("x")!=""&&querystr("y")!="") return true; return false; } }
i have tried use timer( setinterval
) sure data can filled in form after loaded, result isn't different.
the result of b.html should fill input type=hidden
params respectively,
when use b.html params in url, goes well, if use a.html javascript(jquery load()
), result of b.html no params in url.
that is, if operated correctly, input field should be:
<input type="hidden" id="xs" name="x" value="1"> <input type="hidden" id="ys" name="y" value="2">
but in block of a.html, field is
<input type="hidden" id="xs" name="x" > <input type="hidden" id="ys" name="y" >
so, use wrong way of jquery?
any direction or advice appreciate!
try (pattern)
$(function() { $("#rp") .load("b.html", function(data, textstatus) { if (textstatus === "success") { $(this).find("#xs") .prop("value", "1") .siblings("#ys") .prop("value", "2") }; }); });
Comments
Post a Comment