如何使用encodeURIComponent()选择嵌套元素?

时间:2014-10-06 04:26:51

标签: jquery nested traversal elements encodeuricomponent

只是一个快速的问题,有些东西让我的大脑感到震惊。我不能让encodeURIComponent选择嵌套元素,具体来说,

encodeURIComponent($(".signs selected_sign > #s2FaceFinish").val());

只生成'未定义的值' - /online-order2.html?size=1cm%20x%20&width=2cmundefined:

$(function () {
      $("#proceed_button").bind("click", function () {
        var url = "online-order2.html?size="
        + encodeURIComponent($("#height").val())
        + "cm x " + "&width=" 
        + encodeURIComponent($("#width").val()) + "cm" 
        + encodeURIComponent($(".signs selected_sign > #s2FaceFinish").val());
        window.location.href = url;
    });
  });

这是HTML:

<article>
  <form action="" id="sDimensions" onsubmit="return false;">
    <div class="height">
      <input id="height" type="number" maxlength="3" name="SignHeight" value="" />cm height<br>
    </div>
    <div class="width">
      <input id="width" type="number" maxlength="3" name="SignWidth" value="" />cm width<br>
    </div>
  </form>
</article>


<article class="signs selected_sign">
  <form id="s2Options">
    <div class="sign_options>"Face finish: "
      <select name="s2FaceFinish" id="s2FaceFinish">
        <option value="printed">Printed</option>
        <option value="vinyl">vinyl</option>
      </selected>
    </div?
  </form>
</article>

1 个答案:

答案 0 :(得分:0)

修改,更新

选择器selected_sign内的

$(".signs selected_sign > #s2FaceFinish").val() 无效的选择器(html似乎不包含selected_sign元素/标记)?

在下面改为

$(".signs .sign_options > #s2FaceFinish").val()

注意,"cm x "中的空格未在url字符串

中编码

尝试

$(function () {
      $("#proceed_button").bind("click", function () {
         var url = "online-order2.html?size="
        + encodeURIComponent($("#height").val() + "cm x ")
        + "&width=" 
        + encodeURIComponent($("#width").val()) + "cm" 
        + encodeURIComponent($(".signs .sign_options > #s2FaceFinish").val());
        console.log(url);
      });
});

jsfiddle http://jsfiddle.net/guest271314/kwqoyt0j/