0

只是一个快速的问题,它正在破坏我的大脑。我无法让 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>
4

1 回答 1

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/

于 2014-10-06T04:43:29.373 回答