根据下拉列表中选择的状态重定向到网址的Javascript表单

时间:2017-10-24 05:21:59

标签: javascript forms redirect

我没有多少JavaScript经验。只是PHP。我一直在尝试创建一个不会捕获任何信息的JavaScript表单。它只是根据用户在下拉列表中选择的状态将用户重定向到URL。

因此,如果用户在状态下拉列表中输入Alabama,则会将其重定向到设置的URL。因此代码将有50个URL可能的重定向 - 每个状态一个。

我一直在寻找高低,我所做的每一次尝试都是可怕的 - 如果有人对如何做到这一点有任何建议,我将非常感激。

3 个答案:

答案 0 :(得分:0)

<select class="form-control" name="stateId" id="stateId">
<option value="" selected="" disabled="">Select Country</option>
<option value="1">ABC</option>
<option value="2">DEF</option>
<option value="3">GHI</option>

$('#stateId').change(function(){

    // get dropdown value to variable
    var ID = $(this).val();

    // redirect user with following dynamic url
    window.location.href = "localhost/view/steteID/" + ID;

    // or
    window.location.href = "../view/steteID/" + ID;

    // this is optional, if you required ajax call in future
    $.ajax({
        type: "get",
        dataType: "json",
        url: 'path/'+ID,

        success:function(response){
            $('#districtId').empty();
            response[0].subProducts.forEach(function(elem,index){
                $('#districtId').append('<option value="'+ elem.code +'"></option>');
            });
        },
        error:function(xhr){
            console.log(xhr);
        }
    });
});

答案 1 :(得分:0)

您可以编写一个方法,该方法将在更改选择下拉列表时触发,并根据您可以重定向用户的值。

这是 PURE JAVASCRIPT 实施。

document.addEventListener('DOMContentLoaded',function() {
    document.querySelector('select[name="states_select"]').onchange=changeEventHandler;
},false);


function changeEventHandler(event) {
    if(!event.target.value) alert('Please Select state');
    else{
      alert('You like ' + event.target.value);
      //handle the redirect here
      // window.location = "YOUR_REDIRECT_URL";
    }
}
<select name="states_select">
  <option> Select a State </option>
  <option value="State 1"> State 1 </option>
  <option value="State 2"> State 2 </option>
  <option value="State 3"> State 3 </option>
</select>

Read more about the change event here

答案 2 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="websites" >
<select  id="urls">
                <option value="https://google.com">Google</option>
                <option value="https://stackoverflow.com">Stack overflow</option>
                 <option value="https://example.net">Example net</option>
</select>
</form>
row = 1
for f1 in files3:
    if f1.lower().endswith( ('.png', '.jpg', '.jpeg') ):
        try:
            image_path1 = files_dir3 + '/' + f1
            txt = pytesseract.image_to_string( Image.open( image_path1 ) )
            print (txt)
            if txt != '':
                print ('0')
                worksheet4.write( row, 1, '0' )
                worksheet4.write( row, 2, txt )
                worksheet4.write( row, 0, image_path1 )
            else:
                worksheet4.write( row, 1, '1' )
                worksheet4.write( row, 2, 'No Text On Image' )
                worksheet4.write( row, 0, image_path1 )
                image_path3 = files_dir3 + '/' + f1
                img = cv2.imread( image_path3 )
                mask = np.zeros( img.shape[:2], np.uint8 )
                bgdModel = np.zeros( (1, 65), np.float64 )
                fgdModel = np.zeros( (1, 65), np.float64 )
                rect = (50, 50, 450, 290)
                cv2.grabCut( img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT )
                mask2 = np.where( (mask == 2) | (mask == 0), 0, 1 ).astype( 'uint8' )
                img = img * mask2[:, :, np.newaxis]
                # plt.imshow(img),plt.colorbar(),plt.show()
                cv2.imwrite( os.path.join( files_dir1, f1 ), img )
                print ("Image copied: " + f1)
                if f2.lower().endswith( ('.png', '.jpg', '.jpeg') ):
                    # image_path1 = files_dir + '\\' + f
                    image_path2 = files_dir1 + '/' + f2
                    print (f2)
                    txt = pytesseract.image_to_string( Image.open( image_path2 ) )
                    print (txt)
                    if txt != '':
                        print ('0')
                        worksheet4.write( row, 1, '0' )
                        worksheet4.write( row, 2, txt )
                        worksheet4.write( row, 0, image_path2 )
                    else:
                        print ('1')
                        worksheet4.write( row, 1, '1' )
                        worksheet4.write( row, 2, 'No Text On Image' )
                        worksheet4.write( row, 0, image_path2 )
        except:
            print ("Moving On")
    row += 1

在这里你可以实现这个