将输入值转换为变量

时间:2018-10-07 14:44:41

标签: javascript html forms

我想将用户编写的值转换为如下所示的按钮

process.php?fname=$fname&lname=$lname

我写了以下代码

<form id="identification" action="/action_page.php">
  First name: <input type="text" name="fname" value="Mickey"><br>
  Last name: <input type="text" name="lname" value="Mouse"><br><br>
</form> 

<button onclick="getName()">Try it</button>

<p id="result"></p>

<script>
function getName() {
    var x = document.getElementById("identification");
    var text = "";
    var i;
    for (i = 0; i < x.length ;i++) {
        text += x.elements[i].value + "<br>";
    }
    document.getElementById("result").innerHTML = text;
}
</script>

我的问题是如何将给定的按钮转换为带有fname和lname varianles的重定向到网页的按钮

2 个答案:

答案 0 :(得分:0)

你在这里

def get_facial(data):
    face_api_url = 'https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect'

    # Set image_url to the URL of an image that you want to analyze.
    headers = {'Ocp-Apim-Subscription-Key': subscription_key,
    "Content-Type":"application/octet-stream"
    }
    params = {
        'returnFaceId': 'true',
        'returnFaceLandmarks': 'false',
        'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
        'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
    }
    response = requests.post(face_api_url, params=params, headers=headers, data=data)
    faces = response.json()
    res={}
    import pdb; pdb.set_trace()

    res["status"] = '200'
    res["num"] = str(len(faces))
    return res

@app.route('/facial',methods=['POST'])
def facial():
    import pdb; pdb.set_trace()
    data=bytes(request.get_data())
    res={}
    try:
        res = get_facial(data)
    except:
        res['status'] = '404'
    print(res)
    return json.dumps(res)
function getName() {
    var x = document.getElementById("identification");
    var text = "process.php?";
    var i;
    for (i = 0; i < x.length ;i++) {
        text += x.elements[i].name + '=' + x.elements[i].value;
        if(i < x.length - 1) {
            text += '&';
        }
    }
    document.getElementById("result").innerHTML = '<a href="' + text + '"><button>LINK</button></a>';
}

答案 1 :(得分:0)

我完全会质疑您使用此表格的方法,但这是一个简单干净的解决方案:

function getName() {
    var inputs = document.querySelectorAll('#identification input[type="text"]');
    var params = [];
    var url = '/process.php?';

    // Map array of text inputs from the form into an array of name=value strings
    params = [...inputs].map(i => `${i.name}=${i.value}`)
    // Join the array with & and add it to the url string
    url += encodeURI(params.join('&'))

    // window.location = url
    document.querySelector('#result').textContent = url
}

JSFiddle